import {GET_CART_ITEM, DEL_CART_ITEM, CLEAR_CART_DATA} from '../../config/ActionType'
import {fetch, API_GET_CART_ITEM, API_DEL_CART_ITEM, API_SELECT_CART_ITEM, API_SWITCH_CART_ITEM, API_CHANGE_ADDRESS, API_CHANGE_CART_ACT} from '../../config/Api'
import {fetchAddToCart, showMessage, fetchCartCount, getCartCount} from '../../global/actions'
import {hashHistory} from 'react-router'

export const getCartItem = data => ({
  type: GET_CART_ITEM,
  payload: {cart: data}
})

export const clearCartData = () => ({type: CLEAR_CART_DATA})

export function fetchCartItem() {
  return (dispatch, getState) => fetch(API_GET_CART_ITEM)(dispatch, getState).then(json => {
    if (json.success) {
      dispatch(getCartItem(json.data))
    } else {
      dispatch(showMessage(json.message))
    }
  })
}

export function changeCount(params) {
  return (dispatch, getState) => {
    fetchAddToCart(Object.assign({status: '2'}, params))(dispatch, getState)
      .then(json => {
        if (json.success) {
          dispatch(getCartItem(json.data))
          dispatch(getCartCount(json.data.totalCount))
        } else {
          dispatch(showMessage(json.message))
        }
      })
  }
}

export const delCartItem = () => ({type: DEL_CART_ITEM})

// 删除购物车商品，返回购物车列表
export function fetchDelCartItem(params) {
  return (dispatch, getState) => fetch(API_DEL_CART_ITEM, params)(dispatch, getState).then(json => {
    if (json.success) {
      fetchCartCount()(dispatch, getState)
      dispatch(getCartItem(json.data))
    } else {
      dispatch(showMessage(json.message))
    }

    return json
  })
}

// 勾选购物车商品，返回购物车列表
export function fetchSelectCartItem(params) {
  return (dispatch, getState) => fetch(API_SELECT_CART_ITEM, params)(dispatch, getState).then(json => {
    if (json.success) {
      dispatch(getCartCount(json.data.totalCount))
      dispatch(getCartItem(json.data))
    } else {
      dispatch(showMessage(json.message))
    }
  })
}

// 切换个人与企业购物车，返回购物车信息
export function fetchSwitchCartItem(params) {
  return (dispatch, getState) => fetch(API_SWITCH_CART_ITEM, params)(dispatch, getState).then(json => {
    if (json.success) {
      fetchCartCount()(dispatch, getState)
      dispatch(getCartItem(json.data))
    } else {
      dispatch(showMessage(json.message))
    }
  })
}

// 切换配送地址，返回购物车信息
export function fetchChangeAddress(params) {
  return (dispatch, getState) => fetch(API_CHANGE_ADDRESS, params)(dispatch, getState).then(json => {
    if (json.success) {
      fetchCartCount()(dispatch, getState)
      dispatch(getCartItem(json.data))
    } else {
      dispatch(showMessage(json.message))
    }
  })
}

// 更换商品活动和活动赠品,返回购物车信息
export function fetchChangeAct(params) {
  return (dispatch, getState) => fetch(API_CHANGE_CART_ACT, params)(dispatch, getState).then(json => {
    if (json.success) {
      dispatch(getCartItem(json.data))
    } else {
      dispatch(showMessage(json.message))
    }
  })
}

export const actions = {
  fetchCartItem,
  getCartItem,
  delCartItem,
  fetchDelCartItem,
  fetchSelectCartItem,
  changeCount,
  fetchSwitchCartItem,
  fetchChangeAddress,
  fetchChangeAct
}

// Reducers
export const ACTION_HANDLERS = {
  [GET_CART_ITEM]: (state, action) => ({...state, data: action.payload.cart, fetching: false}),
  [CLEAR_CART_DATA]: state => ({...state, data: undefined}),
  [DEL_CART_ITEM]: state => ({...state, fetching: true})
}

const initialState = {
  data: undefined,
  order: undefined,
  voucherList: undefined,
  orderParams: undefined
}

export default function(state = initialState, action) {
  const handler = ACTION_HANDLERS[action.type]

  return handler ? handler(state, action) : state
}
